package insat.challenge.metier;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

public class Place {

	private String name; 
	private String adress;
	private String telephone;
	private String longitude;
	private String latitude;
	private PlaceCategory placeCategory;
	private Vector<Place> places = new Vector<Place>();
	private String codesource = "";

	public String getAdress() {
		return adress;
	}

	public void setAdress(String adress) {
		this.adress = adress;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(String longitude) {
		this.longitude = longitude;
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(String latitude) {
		this.latitude = latitude;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public PlaceCategory getPlaceCategory() {
		return placeCategory;
	}

	public void setPlaceCategory(PlaceCategory placeCategory) {
		this.placeCategory = placeCategory;
	}

	public List<Place> getPlaces() {
		

		return places;
	}

	public void setPlaces(Vector<Place> places) {
		this.places = places;
	}

	public Vector<Place> searchPlaces(PlaceCategory placeCategory,
			List<Place> list) {
		Vector<Place> places = new Vector<Place>();
		for (Place place : list) {
			if (place.placeCategory.getName().equals(placeCategory.getName()))
				places.add(place);
		}

		return places;
	}

	public String toString() {
		return "nom: "+this.getName()+", adresse: "+this.adress;
	}

	public String GetHTML(List<NameValuePair> nvps) {
		DefaultHttpClient httpClient = new DefaultHttpClient();
		String url = "http://helpbox.belhassineamine.com/index.php/api/getplacesforcategory.json";
		try {
			HttpResponse res;
			URI uri = new URI(url);
			if (nvps != null) {
				HttpPost methodpost = new HttpPost(uri);
				methodpost.addHeader("pragma", "no-cache");
				methodpost
						.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
				res = httpClient.execute(methodpost);
			} else {
				HttpGet methodget = new HttpGet(uri);
				methodget.addHeader("pragma", "no-cache");
				res = httpClient.execute(methodget);
			}
			InputStream data = res.getEntity().getContent();

			return generateString(data);

		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "";
	}

	static public String generateString(InputStream stream) {
		InputStreamReader reader = new InputStreamReader(stream);
		BufferedReader buffer = new BufferedReader(reader, 4 * 1024);
		StringBuilder sb = new StringBuilder();
		try {
			String cur;
			while ((cur = buffer.readLine()) != null) {
				sb.append(cur).append("\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			stream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	void lecture(String id) {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
		nameValuePairs.add(new BasicNameValuePair("longitude", "36.843104"));
		nameValuePairs.add(new BasicNameValuePair("latitude", "10.196428"));
		nameValuePairs.add(new BasicNameValuePair("category_id", id));
		nameValuePairs.add(new BasicNameValuePair("rayon", "1000"));
		codesource = GetHTML(nameValuePairs);
		Log.i("codesource", codesource);
	}

	public Vector<Place> searchPlaces1(PlaceCategory placeCategory, List<Place> list) {
		lecture(placeCategory.getId());
		Log.i("l'id de la place",placeCategory.getId());
		Vector<Place> places = new Vector<Place>();
		try {
			
			JSONObject objet = new JSONObject(codesource);
			JSONArray jsonArray = new JSONArray();
			jsonArray = objet.getJSONArray("places");
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				Place place = new Place();
				place.setName(jsonObject.getString("name"));
				place.setAdress(jsonObject.getString("adress"));
				place.setTelephone(jsonObject.getString("telephone"));
				place.setLongitude(jsonObject
						.getString("telephone"));
				place.setLatitude(jsonObject
						.getString("telephone"));
				Log.i("Objetttt", jsonObject.getString("adress"));
				Log.i("Objetttt _ id", jsonObject.getString("telephone"));
				places.add(place);

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//

		return places;
	}

}
